package com.net.xpay.partner.controller.partner;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.util.PhoneUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.enums.config.CommonSysConfigKey;
import com.net.xpay.common.enums.user.ClientFrom;
import com.net.xpay.common.enums.user.UserTag;
import com.net.xpay.common.helper.CommonSysConfigHelper;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.model.RegisterModel;
import com.net.xpay.common.service.UserService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.model.UpdatePartnerModel;
import com.net.xpay.partner.po.UpdateUserSecurityStatusPo;
import com.net.xpay.partner.secutiry.RequestUser;
import com.net.xpay.partner.vo.UserRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@Slf4j
@RestController
@RequestMapping("/partner/user")
@Api(tags = "合伙人/商户管理", description = "合伙人 商户管理 zc")
public class UserController {

    @Autowired
    private UserManager userManager;

    @Autowired
    private UserService userService;

    @Autowired
    private CommonSysConfigHelper commonSysConfigHelper;



    @ApiOperation("修改用户状态")
    @PostMapping("/update-user-security-status")
    @ResponseBody
    public ResultData<User> updateUserSecurityStatus(@ApiIgnore @RequestUser Partner partner, @RequestBody UpdateUserSecurityStatusPo po) {
        Preconditions.checkArgument(po.getId() != null && po.getSecurityStatus() != null);
        if (Strings.isNullOrEmpty(po.getSecurityRemark())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入备注");
        }
        if (po.getSecurityRemark().length() > 32) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "备注不超过32个字");
        }
        User user = userManager.getById(po.getId());
        if (user.getPartnerId() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "非法操作");
        }
        if (!user.getPartnerId().equals(partner.getId())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "非法操作");
        }
        user.setSecurityStatus(po.getSecurityStatus());
        user.setSecurityRemark(po.getSecurityRemark());
        userManager.updateByPrimaryKeyWithVersion(user);
        return new ResultData<>(user);
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    @ResponseBody
    public ResultData<UserRegisterVo> register(@RequestBody RegisterModel registerModel) {
        //验证手机号
        if (!PhoneUtil.isEffectiveMobile(registerModel.getMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号格式不正确");
        }
        //确认手机号未被注册
        if (userManager.findByMobile(registerModel.getMobile()) != null) {
            throw new BusinessException(PoseidonErrorCode.MOBILE_DUPLICATED, "手机号已经被注册,你可以使用找回密码功能");
        }

        String randomPassword = Strings.padStart("" + RandomUtils.nextInt(0, 1000000), 6, '0');
        registerModel.setPassword(randomPassword);
        registerModel.setClientFrom(ClientFrom.PC_WEB);
        registerModel.setAdminRegister(true);
        registerModel.setUserTag(UserTag.PLATFORM);
        User user = userService.registerInternal(registerModel);

        String website = commonSysConfigHelper.getString(CommonSysConfigKey.MERCHANT_DOMAIN);
        String openDomain = commonSysConfigHelper.getString(CommonSysConfigKey.OPEN_DOMAIN);
        String openapiDomain = commonSysConfigHelper.getString(CommonSysConfigKey.OPENAPI_DOMAIN);
        UserRegisterVo vo = new UserRegisterVo();
        vo.setMobile(user.getMobile());
        vo.setPassword(randomPassword);
        vo.setWebsite(website);
        vo.setOpenDomain(openDomain);
        vo.setOpenapiDomain(openapiDomain);

        // 绑定商户
        UpdatePartnerModel model = new UpdatePartnerModel();
        // 渠道ID
        model.setPartnerId(registerModel.getChannelMasterUserId());
        model.setId(user.getId());
        userService.updatePartner(model);

        return new ResultData<>(vo);
    }
}
